package com.yuyang.gulimall.ware.listener;

import com.rabbitmq.client.Channel;
import com.yuyang.common.to.mq.OrderTo;
import com.yuyang.common.to.mq.SkuLockedTo;
import com.yuyang.gulimall.ware.service.WareSkuService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author yu yang
 * @date 2023/2/9 -15:30
 */
@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {
    @Autowired
    WareSkuService wareSkuService;
    @RabbitHandler
    public void handleStockRelease(SkuLockedTo to, Message message, Channel channel) throws IOException {
        System.out.println("收到解锁消息");
        try {
            wareSkuService.releaseStock(to);
            //执行成功接受消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //发回队列,等待其他队列消费
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
    @RabbitHandler
    public void handleStockRelease(OrderTo to, Message message, Channel channel) throws IOException {
        System.out.println("订单已取消，收到库存解锁消息");
        try {
            wareSkuService.releaseStock(to);
            //执行成功接受消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //发回队列,等待其他队列消费
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

}
